<?php 
require_once("abstractdatabase.php");

class loginclass extends MsssqlDB
{
	
	private $userlogin;
	private $passlogin;
	
	public function __construct($usernames,$password)
	 {
		 parent::__construct("mssql.infratechastm.com","infratechastm_dede","system1","infratechastm_Mockup");
		 $this->logincheck($usernames,$password);
		 
	 }
	 
	private function logincheck($usernames,$password)
	{
		
		$this->userlogin = $usernames;
		$this->passlogin = $password;
		
		$queryrow = $this->msssqlselectdata("CAST(sUserName as text) as sUserName,fkRole","Usernames","sUserName = '$this->userlogin' and sLoweredUserName = '$this->passlogin'");
		//echo $queryrow;\
		$rowq = $this->msssqlnumrows($queryrow);
		echo $rowq;
		if ( $rowq == 0)
		{
			
			$_SESSION['code_errorlogin'] = "Password And Usernames Fail";
			
			}else{
				
			$loginquery = $this->mssqlfetcharray($queryrow);
			$_SESSION['user'] = $loginquery['sUserName'];
			$_SESSION['type'] = $loginquery['fkRole'];
			$_SESSION['projectname'] = $loginquery['projecttype'];	
			
	
			//echo $_SESSION['type'];
			
				// Checktype 
				
				if($_SESSION['type'] == 1)
				{
						echo "<meta http-equiv='refresh' content='0;URL=admin.php'>";	
									
					}else if ($_SESSION['type'] == 2)
					{	
						echo "<meta http-equiv='refresh' content='0;URL=projectsite.php'>";		
					}
					else if ($_SESSION['type'] == 3)
					{	
						echo "<meta http-equiv='refresh' content='0;URL=silde.php'>";		
					}

			}
			
		}
	 
	
	public function adduser($names,$lastname,$mails,$tel,$workp,$useradd,$passadd,$passcon,$pz)
	{
		
		if(!empty($names) and !empty($lastname) and !empty($mails) and !empty($tel) and !empty($workp)
		and !empty($useradd) and !empty($passadd) and !empty($passcon)and !empty($pz)){
		
				if($passadd == $passcon){
				
					$chnames = $useradd;
					
						$input = "'$names','$lastname','$mails','$tel','$workp','$useradd','$passadd','$pz','2'";
						
						$checknames = $this->mysqlnumrow($this->selectdatasql("*","usersystem","usernamelogin = '$chnames'"));
					
							if($checknames == 0 ){
								
								$query = $this->insertdatasql("name,lastname,email,Tel,Workplace,usernamelogin,passlogin,projaecttype,usertype","`usersystem`",$input);
								
								if(!$query){
									
									//print $input;
											echo "Record already exist.<br>";
									
									}else{
										$this->sendmail($mails);
										// echo "<meta http-equiv='refresh' content='0;URL=user.php'>";		
										
										}
							}else{
								
								$_SESSION['code_error'] = "Usernames นี้ถูกใช้เป็นแล้วน";

							}
			
					}else{
							
						$_SESSION['code_error'] = "กรุณากรอ Password ให้ตรงกัน";
					}

			}else{
				
				$_SESSION['code_error'] = "กรุณากรอ ข้อมูลให้ครบ";
				//session_register("code_error");
				//echo "<meta http-equiv='refresh' content='0;URL=adduser.php'>";
				
			}
		
		}

	
	public function viewallusername()
	{
		return $this->selectdatasql("*","usersystem","");
	}
		
		
		public function viewdata($id)
		{	
			$query = $this->selectdatasql("*","usersystem","auto_id = '$id'");
			
			return $this->mysqlfetcharray($query);
		}
		
		public function editdatauser($id,$names,$lastname,$mails,$tel,$workp,$useradd,$passadd)
		{
			if(!empty($id) and !empty($names) and !empty($lastname) and !empty($mails) and !empty($tel) and !empty($workp)
				and !empty($useradd) and !empty($passadd))
				
				{
					
					$dataupdate = "name = $names,
					lastname = $lastname,
					email = $mail,
					Tel = $tel,
					Workplace = $workp,
					usernamelogin = $useradd,
					passlogin = $passadd
					";
				
				   $query = $this->updatedatasql("usersystem","$dataupdate","auto_id = '$id'");
					if(!$query)
					{

					}else{
						echo "Dupate data Fis";
						
					}
				
			    }else{
					
					
				}
		}
		
		
		
		
	
		
		

	}

?>